package com.ywm.eduService.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywm.commonUtil.JwtUtils;
import com.ywm.commonUtil.R;
import com.ywm.commonUtil.orderVo.CourseWebVoOrder;
import com.ywm.eduService.client.OrderClient;
import com.ywm.eduService.client.VodClient;
import com.ywm.eduService.entity.EduCourse;
import com.ywm.eduService.entity.frontVo.CourseFrontVo;
import com.ywm.eduService.entity.frontVo.CourseWebVo;
import com.ywm.eduService.entity.vo.ChapterVo;
import com.ywm.eduService.service.EduChapterService;
import com.ywm.eduService.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/eduservice/cousefront")
@RestController
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private VodClient vodClient;
    //条件查询带分页
    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo CourseFrontVo) {
        Page<EduCourse> page1 = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(page1, CourseFrontVo);
        return R.ok().data(map);
    }

    //前台课程详情页
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        //根据课程id，查询章节和小节
        List<ChapterVo> courseList = chapterService.getChapterVideoByCourseId(courseId);
        //根据课程id和用户id查询当前课程是否支付
        System.out.println("==>"+courseId);
        System.out.println("-->"+JwtUtils.getMemberIdByJwtToken(request));
        boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("courseList",courseList).data("isBuy",buyCourse);
    }
    //根据课程ID查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder webVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,webVoOrder);
        return webVoOrder;
    }

    @GetMapping("test2")
    public R test2(){
        return orderClient.test();
    }

    @GetMapping("test3")
    public R test3(){
        return vodClient.test();
    }
}
